<template>
  <div>
    <el-card class="box-card">
      <h2>编辑客户</h2>
      <el-form :model="customerForm" label-width="120px">
        <el-form-item label="选择客户：">
          <el-select v-model="selectedCustomerName" @change="fetchCustomerDetailsByName" placeholder="请选择客户">
            <el-option
              v-for="customer in customers"
              :key="customer.id"
              :label="customer.name"
              :value="customer.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称："required>
          <el-input v-model="customerForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话："required>
          <el-input v-model="customerForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址："required>
          <el-input v-model="customerForm.address"></el-input>
        </el-form-item>
        <el-form-item label="传真：">
          <el-input v-model="customerForm.fax"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="customerForm.email" type="email"></el-input>
        </el-form-item>
        <el-form-item label="银行名称：">
          <el-input v-model="customerForm.bankName"></el-input>
        </el-form-item>
        <el-form-item label="银行账号：">
          <el-input v-model="customerForm.bankAccount"></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input v-model="customerForm.remarks" type="textarea"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
             <el-button @click="resetForm('customerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      customerForm: {
        name: '',
        phone: '',
        address: '',
        fax: '',
        email: '',
        bankName: '',
        bankAccount: '',
        remarks: ''
      },
      customers: [],
      selectedCustomerName: null
    };
  },
  created() {
    this.fetchCustomers();
  },
  methods: {
    fetchCustomers() {
      this.axios.get('/api/customer/get')
        .then(response => {
          const result = response.data;
          if (Array.isArray(result.data)) {
            this.customers = result.data;
          } else {
            console.error('Error: Expected an array but got:', result.data);
          }
        })
        .catch(error => {
          console.error('Error fetching customers:', error);
        });
    },
    fetchCustomerDetailsByName() {
      if (!this.selectedCustomerName) return;
      const customer = this.customers.find(c => c.name === this.selectedCustomerName);
      if (customer) {
        this.axios.get(`/api/customer/search?name=${customer.name}`)
          .then(response => {
            console.log(response.data);  // 检查响应数据
            const customerDetails = response.data.data;
       const customer= customerDetails[0]; // 获取数组中的第一个元素
            this.customerForm = {

              name: customer.name,
              phone: customer.phone,
              address: customer.address,
              fax: customer.fax,
              email: customer.email,
              bankName: customer.bankName,
              bankAccount: customer.bankAccount,
              remarks: customer.remarks
            };
          })
          .catch(error => {
            console.error('Error fetching customer details:', error);
          });
      }
    },
   submitForm() {
     console.log('Submitting form with data:', this.customerForm);  // 打印提交的数据
     const customer = this.customers.find(c => c.name === this.selectedCustomerName);
     if (customer) {
       console.log('Found customer:', customer);  // 打印找到的客户信息
       this.axios.put(`/api/customer/edit/${customer.name}`, this.customerForm)
         .then(response => {
           console.log('Server response:', response);  // 打印服务器响应
           this.$message({
             message: '客户更新成功',
             type: 'success'
           });
           this.$router.push('/list_customer');
         })
         .catch(error => {
           console.error('Error updating customer:', error);
           console.error('Error response:', error.response);  // 打印错误响应
           this.$message.error('客户更新失败');
         });
     } else {
       console.error('No customer found with name:', this.selectedCustomerName);  // 如果没有找到客户
     }
   },
      resetForm() {
         // 重置表单
         this.customerForm = {
           newCustomerName:'',
                        phone: '',
                        address: '',
                        fax: '',
                        email: '',
                        bankName: '',
                        bankAccount: '',
                        remarks: ''
         };
       }

  }
};
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}
.el-form-item {
  margin-bottom: 20px;
}
</style>
